import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

import '../../../module_common/provider/wallet_info_provider.dart';
import '../../../module_common/widgets/blockies.dart';

class WalletListItem extends StatelessWidget {
  final Wallet wallet;
  final bool sortMode;
  final VoidCallback? topCallback;

  const WalletListItem({super.key, required this.wallet, required this.sortMode, this.topCallback});

  _selectWallet(BuildContext context, Wallet wallet) {
    WalletInfo? walletInfo = ObjectBox().walletInfoModel.getWalletInfoById(SP().getWalletSelectedInfoId(wallet.id));
    walletInfo ??= ObjectBox().walletInfoModel.getFirstWalletInfo(wallet);
    if (null != walletInfo) {
      context.read<WalletInfoNotifier>().updateCurrentWallet(walletInfo);
      Navigator.of(context).pop();
    } else {
      // EasyLoading.showError(
      //     LocaleKeys.dangqiannweishengchengrenheliandeqianbao.tr());
    }
  }

  @override
  Widget build(BuildContext context) {
    bool selected = context.read<WalletInfoNotifier>().walletId == wallet.id;
    return Container(
      padding: const EdgeInsets.only(left: 20, right: 0),
      height: 60,
      color: selected ? Color.fromRGBO(246, 246, 246, 1) : Colors.transparent,
      child: InkWell(
        onTap: () {
          if (sortMode) {
            return;
          }
          _selectWallet(context, wallet);
        },
        child: Row(
          children: [
            SizedBox(
              width: 54.w,
              height: 42.w,
              child: Stack(
                children: [
                  UserAvatar(
                    walletId: 999 - wallet.id,
                    size: 40.w,
                  ),
                  Positioned(
                      right: 6.w,
                      bottom: 0,
                      child: Visibility(
                          visible: selected,
                          child: Container(
                              width: 18.w,
                              height: 18.w,
                              child: Image.asset('assets/images/wallet_assets/wallet_select.png')))),
                ],
              ),
            ),
            Expanded(
              child: Column(
                children: [
                  const SizedBox(
                    height: 10,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        wallet.getName(),
                        style: TextStyle(
                            color: Color.fromRGBO(51, 51, 51, 1),
                            fontSize: 14.sp,
                            height: 20.w / 14.w,
                            fontWeight: FontWeight.w500),
                      ),
                      Text(
                        wallet.getName(),
                        style: TextStyle(
                            color: Color.fromRGBO(51, 51, 51, 1),
                            fontSize: 14.sp,
                            height: 20.w / 14.w,
                            fontWeight: FontWeight.w500),
                      ),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        wallet.getName(),
                        style: TextStyle(
                            color: Color.fromRGBO(190, 189, 202, 1),
                            fontSize: 12.sp,
                            height: 14.w / 12.w,
                            fontWeight: FontWeight.bold),
                      ),
                      Text(
                        wallet.getName(),
                        style: TextStyle(
                            color: Color.fromRGBO(190, 189, 202, 1),
                            fontSize: 12.sp,
                            height: 14.w / 12.w,
                            fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 10.w,
                  ),
                ],
              ),
            ),
            SizedBox(
              width: 10.w,
            ),
            GestureDetector(
              onTap: () {},
              child: Container(
                padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 11.w, bottom: 11.w),
                child: PublicImage(
                  image: 'assets/images/wallet_assets/select_wallet_detail.png',
                  width: 4.w,
                  height: 18.w,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
